package com.xxx.blog.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.mapper.ArticleTypeMapper;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;
import com.xxx.blog.vo.ArticleTypeVo;
import com.xxx.blog.vo.GdVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service//
public class ArticleTypeServiceImpl implements IArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Override
    public List<ArticleType> findAll() {
        return articleTypeMapper.findAll();
    }

    @Override
    public ArticleType findOne(Long id) {
        return articleTypeMapper.findOne(id);
    }

    @Override
    public List<ArticleType> queryPage(ArticleTypeQuery articleTypeQuery) {
        PageHelper.startPage(articleTypeQuery.getCurrentPage(), articleTypeQuery.getPageSize());

        return articleTypeMapper.queryPage(articleTypeQuery);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void addArticleType(ArticleType articleType) {
        ////加密业务
        //String salt = RandomUtil.randomString(32);
        //articleType.setSalt(salt);//盐水保存到数据库是为了后面做对比
        //
        //articleType.setPassword(SecureUtil.md5(articleType.getPassword()+salt));//核心代码,其他缺啥补啥,加盐加密

        articleTypeMapper.addArticleType(articleType);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void updateArticleType(ArticleType articleType) {
        articleTypeMapper.updateArticleType(articleType);
    }

    //alt shif p
    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void deleteArticleType(Long id) {
        articleTypeMapper.deleteArticleType(id);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void batchArticleTypes(Long[] ids) {
        articleTypeMapper.batchArticleTypes(ids);
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Cacheable(cacheNames = "articleType", key = "'typeTree'")
    public List<ArticleType> getTypeTree() {
        List<ArticleType> typeList = (List<ArticleType>) redisTemplate.boundValueOps("typeList").get();
        if (typeList != null) {
            System.out.println("数据来自redis");
            return typeList;
        }
        System.out.println("数据来自mysql");
        typeList = this.getArticleTypes();
        redisTemplate.boundValueOps("typeList").set(typeList);
        return typeList;
    }

    @NotNull
    private List<ArticleType> getArticleTypes() {
        List<ArticleType> typeTree = new ArrayList<ArticleType>();
        List<ArticleType> articleTypeList = articleTypeMapper.findAll();
        Map<Long,ArticleType> map = new HashMap<>();
        for (ArticleType articleType : articleTypeList) {
            map.put(articleType.getId(),articleType);
        }
        for (ArticleType articleType : articleTypeList) {
            Long parentId = articleType.getParentId();
            if(parentId == null){
                typeTree.add(articleType);
            }else {
                ArticleType articleTypeParent = map.get(parentId);
                articleTypeParent.getChildren().add(articleType);
            }
        }
        return typeTree;
    }

    @Override
    public List<ArticleTypeVo> getArticleTypeStatistics() {
        return articleTypeMapper.getArticleTypeStatistics();
    }


}
